$(function () {
  // 轮播图--------------------------------------start
  // 小轮播图

  var swiper1 = new Swiper(".mySwiper1", {
    slidesPerView: 2,
    spaceBetween: 30,
    slidesPerGroup: 2,
    loop: true,
    loopFillGroupWithBlank: true,
    pagination: {
      el: ".swiper-pagination",
      clickable: true,
    },
    navigation: {
      nextEl: ".swiper-button-next",
      prevEl: ".swiper-button-prev",
    },
    autoplay: {
      delay: 3000,
      disableOnInteraction: false,
    },
  });

  // 大轮播图
  var swiper = new Swiper(".mySwiper2", {
    spaceBetween: 30,
    centeredSlides: true,
    loop: true,
    autoplay: {
      delay: 2500,
      disableOnInteraction: false,
    },
    pagination: {
      el: ".swiper-pagination",
      clickable: true,
    },
    navigation: {
      nextEl: ".swiper-button-next",
      prevEl: ".swiper-button-prev",
    },
  });
  //轮播图样式结束-------------------------------------end
  //给用户id为74651的首页添加数据
  /* for (let i = 1; i < 11; i++) {
  $.get(
    "http://jx.xuzhixiang.top/ap/api/goods/goods-add.php",
    {
      pimg: i + ".webp",
      pname: "商品" + i,
      pprice: i + "00",
      pdesc: "后决定恢复阶段划分等级",
      uid: 74651,
    },
    (res) => {
      console.log(res);
    }
  );
} */

  //http://jx.xuzhixiang.top/ap/api/productlist.php
  // console.log(res.data.data[0].pid)
  //将首页数据渲染到页面上
  // function xq(pid) {
  //   location.href = `detail.html?pid=${pid}`;
  //   console.log(pid)
  // }

  axios
    .get("http://jx.xuzhixiang.top/ap/api/productlist.php", {
      params: { uid: 74651 },
    })
    .then(function (res) {
      //console.log(res);
      let data = res.data.data;
      // console.log(data);
      data = data.slice(0, 10);
      // console.log(data);
      var str = "";
      $(data).each(function (i, ele) {
        // console.log(ele.pimg);
        str += `
        <div class="supermarket_li" data-id=${ele.pid}>
            <a href="#">
              <img src="${ele.pimg}" alt="">
              <p>${ele.pdesc}</p>
              <span>￥</span><span>${ele.pprice}</span>
            </a>
          </div>        
        `;
      });
      // console.log(str);
      $(".supermarket_con").append(str);
    })
    .then(function () {
      $(".supermarket_li").on("click", function () {
        str = "";
        let a = Number($(this).attr("data-id"));
        console.log(a);

        axios
          .get("http://jx.xuzhixiang.top/ap/api/detail.php", {
            params: { id: a },
          })
          .then(function (res) {
            console.log(res);
            location.href = `detail.html?pid=${res.data.data.pid}`;
          });
      });
    });

  //从路径里面获取由首页点击进入详情页的商品id
  function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    }
    return false;
  }
  var a = getQueryVariable("uname");
  console.log(a);
  console.log(
    $(".user_message")
      .find("span")
      .text("hi" + "  " + a)
  );
});
